using System;
using System.Collections;
using MySql.Data.MySqlClient;

namespace bOS.DataBase
{
	public class SelectionValue : bOS.DataBase.bOSTable
	{
		private string				_Id;
		private string				_Text;

		/** CONSTRUCTORS **/

		public SelectionValue(){}

		public SelectionValue(
			string Id,
			string Text
			)
		{
			_Id = Id;
			_Text = Text;
		}

		/** PROPERTIES **/

		public string Id
		{
			get{ return _Id; }
			set{ _Id = value; }
		}
		
		public string Text
		{
			get{ return _Text; }
			set{ _Text = value; }
		}

		public void ReadFromDataBase(MySqlDataReader reader)
		{
			this.Id = reader.GetString(0);
			this.Text = reader.GetString(1);
		}
	}

	
	public class SelectionValuesCollection : CollectionBase, bOS.DataBase.bOSTable
	{
		public SelectionValuesCollection()
		{
		}

		public SelectionValue GetById(string sId)
		{
			SelectionValue theValue = null;
			for ( int i=0; i<this.Count;i++)
			{
				theValue = this[i];

				if ( theValue.Id.CompareTo ( sId) == 0 )
					return theValue;
			}

			return null;
		}

		/** COLLECTION METHODS **/

		public SelectionValue this[ int index ] 
		{
			get  { return( (SelectionValue) List[index] );}
			set  { List[index] = value;}
		}

		public int Add( SelectionValue value ) 
		{
			return( List.Add( value ) );
		}

		public int IndexOf( SelectionValue value ) 
		{
			return( List.IndexOf( value ) );
		}

		public void Insert( int index, SelectionValue value ) 
		{
			List.Insert( index, value );
		}

		public void Remove( SelectionValue value ) 
		{
			List.Remove( value );
		}

		public bool Contains( SelectionValue value ) 
		{
			// If value is not of type SelectionValue, this will return false.
			return( List.Contains( value ) );
		}

		public void ReadFromDataBase(MySqlDataReader reader)
		{
			SelectionValue bo = null;

			while ( reader.Read() )
			{
				bo = new SelectionValue ();

				bo.ReadFromDataBase ( reader );

				this.Add ( bo );
			}
		}

		public enum SelectionValueSortFields
		{
			Text
		}

		/** SORTING METHODS **/


		public void Sort(SelectionValueSortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SelectionValueSortFields.Text:
					InnerList.Sort(new SelectionValueComparer());
					break;
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class SelectionValueComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				SelectionValue first = (SelectionValue) x;
				SelectionValue second = (SelectionValue) y;
				return first.Text.CompareTo(second.Text);
			}
		}

	}
}
